package com.coffee.admin.service.platform.impl;

import com.coffee.admin.api.platform.query.RechargeQuery;
import com.coffee.admin.api.platform.result.RechargeRuleResult;
import com.coffee.admin.api.platform.service.RechargeRuleService;
import com.coffee.admin.service.platform.bean.RechargeRule;
import com.coffee.admin.service.platform.dao.RechargeRuleDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.exception.ValidateBusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@DubboService(dynamic = true)
public class RechargeRuleServiceImpl implements RechargeRuleService {

    @Autowired
    private RechargeRuleDao rechargeRuleDao;

    @Override
    @Transactional
    public Integer saveRechargeRule(RechargeQuery rule) throws BusinessException{
        RechargeRule rechargeRule=new RechargeRule();
        BeanUtilExt.copyProperties(rechargeRule, rule);
        RechargeRule rechargeRule1 = rechargeRuleDao.selectByRechargeAmountOne(rechargeRule.getRechargeAmount());
        ValidateBusinessException.assertNull(rechargeRule1, ExceptionEnum.ERROR_2100);
        // 设置创建时间为当前时间
        rechargeRule.setCreateTime(new Date());
        rechargeRuleDao.insert(rechargeRule);
        return rule.getId();
    }

    @Override
    @Transactional
    public boolean updateRechargeRule(RechargeQuery rule) throws BusinessException{
        RechargeRule rechargeRule=new RechargeRule();
        BeanUtilExt.copyProperties(rechargeRule, rule);
        RechargeRule rechargeRule1 = rechargeRuleDao.selectByRechargeAmountOne(rechargeRule.getRechargeAmount());
        ValidateBusinessException.assertFalse(rechargeRule1!=null&&!rechargeRule1.getId().equals(rule.getId()), ExceptionEnum.ERROR_2100);
        return rechargeRuleDao.update(rechargeRule) > 0;
    }

    @Override
    @Transactional
    public boolean deleteRechargeRule(Integer id) {
        return rechargeRuleDao.deleteById(id) > 0;
    }

    @Override
    public RechargeRuleResult getById(Integer id) {
        RechargeRuleResult rechargeRuleResult=new RechargeRuleResult();
        RechargeRule rechargeRule = rechargeRuleDao.selectById(id);
        BeanUtilExt.copyProperties(rechargeRuleResult, rechargeRule);
        return rechargeRuleResult;
    }

    @Override
    public Page<RechargeRuleResult> listAllRules(PagerInfo pagerInfo) {
        Page<RechargeRuleResult> page=new Page<>(pagerInfo.getStart(), pagerInfo.getPageSize());
        Integer count = rechargeRuleDao.selectCount();
        page.setTotalPage(count);
        List<RechargeRule> rechargeRules = rechargeRuleDao.selectByPage(pagerInfo.getStart(), pagerInfo.getPageSize());
        List<RechargeRuleResult> rechargeRuleResults = DTOUtils.convertList(rechargeRules, RechargeRuleResult.class);
        page.setData(rechargeRuleResults);
        return page;
    }

    @Override
    public RechargeRuleResult matchRule(Double amount) {
        RechargeRuleResult rechargeRuleResult=new RechargeRuleResult();
        RechargeRule rechargeRule = rechargeRuleDao.selectAll().stream()
                .filter(rule -> amount.compareTo(rule.getRechargeAmount()) >= 0)
                .max(Comparator.comparing(RechargeRule::getRechargeAmount))
                .orElse(null);
        BeanUtilExt.copyProperties(rechargeRuleResult, rechargeRule);
        return rechargeRuleResult;

    }

    // 可选：数据校验逻辑
    private void validateRule(RechargeRule rule) {
        if (rule.getRechargeAmount().compareTo(0.0) <= 0) {
            throw new IllegalArgumentException("充值金额必须大于0");
        }
        if (rule.getGiftAmount().compareTo(0.0) < 0) {
            throw new IllegalArgumentException("赠送金额不能为负数");
        }
    }
}